//
//  detailWebViewController.m
//  LazyCat
//
//  Created by rocky on 16/1/6.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "detailWebViewController.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"

@interface detailWebViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    UIWebView *_webV;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    BOOL _finishLoad;//web页是否加载完毕
    UIView *_noNetView;
    UIButton *cancelBtn;
}

@end

@implementation detailWebViewController

- (void)backAction:(UIButton *)btn {
    if (![_webV canGoBack]) {//判断是否可以HTML返回
        if (!self.navigationController.viewControllers) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else {
        [_webV goBack];
        if (!cancelBtn) {
            cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            cancelBtn.frame = CGRectMake(20, 20, 60, 40);
            cancelBtn.titleLabel.font = [UIFont fontWithName:nil size:15];
            [cancelBtn setTitle:@"关闭" forState:UIControlStateNormal];
            [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cancelBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
            [self.navBar addSubview:cancelBtn];
        }
    }
}
-(void)initNav:(NSString*)title doBack:(BOOL)doBack {
    self.navBar.hidden=NO;
    self.titleLable.text=title;
    self.titleLable.textColor = [UIColor whiteColor];
    if (doBack) {
        self.backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        self.backBtn.frame=CGRectMake(8, 20+(44-21)/2, 12, 21);
        [self.backBtn setNormalImageWith:@"lALOCzPrgiYW_22_38"];
        [self.backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.backBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -14, -14, -25)];
        [self.navBar addSubview:self.backBtn];
    }
}
//改变状态栏颜色为白色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)backAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navBar addSubview:_progressView];
    self.navBar.backgroundColor = SetColor(0, 140, 206, 1);
    if (kNetworkNotReachability) {
        _webV.hidden = YES;
        _noNetView.hidden = NO;
    }else{
        _webV.hidden = NO;
        _noNetView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:self.titlename doBack:YES];
    UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    refreshBtn.frame = CGRectMake(ScreenWidth-8-18, 20+(44-18)/2, 18, 18);
    refreshBtn.hidden = YES;
    [refreshBtn setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
    [refreshBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -20, -10, -10)];
    [refreshBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:refreshBtn];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.alpha = 0.2;
    lineV.backgroundColor = [UIColor lightGrayColor];
    [self.navBar addSubview:lineV];
    _webV = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    _webV.delegate = self;
//    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
//    NSString *iptype = [userDefaul objectForKey:@"iptype"];
//    NSString *banben = [userDefaul objectForKey:@"banben"];
//    NSString *url = [NSString stringWithFormat:dataStatisticsUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
    //NSString *url = @"http://www.sina.com";
    [_webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]]];
    _webV.backgroundColor = BackColor;
    [self.view addSubview:_webV];
    _webV.scrollView.showsVerticalScrollIndicator = NO;
#pragma mark -- 进度条1
    _progressProxy = [[NJKWebViewProgress alloc]init];
    _webV.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    CGRect navBounds = self.navBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, 0, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth - ScreenWidth/2+80, 2);
    } completion:^(BOOL finished) {
        //        if (_finishLoad) {
        //            _progressView.hidden = YES;
        //        }
    }];
    _noNetView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
    _noNetView.hidden = YES;
    _noNetView.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:243 / 255.0 blue:247 / 255.0 alpha:1];
    [self.view addSubview:_noNetView];
    UIImageView *netImg = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-100)/2, 60, 100, 90)];
    netImg.image = [UIImage imageNamed:@"无网络_r1_c1"];
    [_noNetView addSubview:netImg];
    UILabel *netFail = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, ScreenWidth, 20)];
    netFail.text = @"网络请求失败";
    netFail.textAlignment = NSTextAlignmentCenter;
    netFail.font = [UIFont systemFontOfSize:16];
    [_noNetView addSubview:netFail];
    UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-120)/2, 200, 120, 40)];
    [refresh setImage:[UIImage imageNamed:@"无网络_r3_c2 下午4.23.04"] forState:UIControlStateNormal];
    refresh.adjustsImageWhenHighlighted = NO;
    [_noNetView addSubview:refresh];
    [refresh addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
}
-(void)refresh {//刷新方法
    if (kNetworkNotReachability) {
        [[MessShowView share] showWithMess:@"当前网络不给力"];
        _noNetView.hidden = NO;
        _webV.hidden = YES;
    }else {
        _noNetView.hidden = YES;
        _webV.hidden = NO;
//        NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
//        NSString *iptype = [userDefaul objectForKey:@"iptype"];
//        NSString *banben = [userDefaul objectForKey:@"banben"];
//        NSString *url = [NSString stringWithFormat:dataStatisticsUrl,[AppData share].username,[Utils md5SignStr],iptype,banben];
        NSString *url = @"http://www.sina.com";
        [_webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    }
}
#pragma mark -- 进度条2
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [UIView animateWithDuration:1 animations:^{
        _progressView.frame = CGRectMake(_progressView.frame.origin.x, _progressView.frame.origin.y, ScreenWidth, 2);
    } completion:^(BOOL finished) {
        _finishLoad = YES;
        _progressView.hidden = YES;
    }];
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    _progressView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
